/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Boundaries.Form_Utama;
import Boundaries.InternalFrame_Servis;
import Entities.Kendaraan;
import Entities.Servis;
import TableModel.Kendaraan_List;
import TableModel.Kendaraan_List_TableModel;
import TableModel.Servis_List;
import TableModel.Servis_TableModel;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Tea-Nose
 */
public class Servis_Controller {

    public void cek_Servis(InternalFrame_Servis frame_Servis) {
        /*
         * JGN ADA YANG EDIT @ADEN
         * 
         */
        try {
//            InternalFrame_Servis form_Servis = (InternalFrame_Servis) frame;
            Servis servis = new Servis();
            Kendaraan kendaraan = new Kendaraan();

            //         java.sql.Date date=new java.sql.Date(form_Servis.dateChooser_TanggalServis.getDate().getDate());
//         int date=form_Servis.dateChooser_TanggalServis.getDate().getDate();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            dateFormat.format(frame_Servis.dateChooser_TanggalServis.getDate());


            //String vBiayaServis = frame_Servis.tf_BiayaServis.getText();
            boolean vStsOli;

            if (frame_Servis.comboBox_StatusOli.getSelectedItem().toString().matches("YA")) {
                vStsOli = true;
            } else {
                vStsOli = false;
            }
            if (frame_Servis.tf_KodeServis.getText().matches("") || frame_Servis.tf_KodeKendaraan.getText().matches("") || frame_Servis.ta_CatatanServis.getText().matches("") /*|| vBiayaServis.matches("")*/) {
                JOptionPane.showMessageDialog(null, "Data Harus Terisi Semua");
            } else {
                try {
                    kendaraan.setNo_polisi(frame_Servis.tf_KodeKendaraan.getText());
                } catch (Exception ex) {
                    Logger.getLogger(Servis_Controller.class.getName()).log(Level.SEVERE, null, ex);
                }
//                kendaraan.setStatus_kendaraan(frame_Servis.comboBox_StatusKend.getSelectedItem().toString());

                String vKodeServis = frame_Servis.tf_KodeServis.getText();
                String vNoPolisi = frame_Servis.tf_KodeKendaraan.getText();
                String vTanggalServis = dateFormat.format(frame_Servis.dateChooser_TanggalServis.getDate());
                String vStatusOli = frame_Servis.comboBox_StatusOli.getSelectedItem().toString();
                String vCatatan_Servis = frame_Servis.ta_CatatanServis.getText();

                servis.setKode_servis(vKodeServis);
                servis.setNo_polisi(vNoPolisi);
                servis.setTanggal_servis(vTanggalServis);
                servis.setStatus_oli(vStsOli);
                servis.setCatatan_servis(vCatatan_Servis);
                //servis.setBiaya_servis(Integer.parseInt(vBiayaServis));

                int confirm = JOptionPane.showConfirmDialog(null, "Kode Servis: " + vKodeServis + "\n"
                        + "No Polisi: " + vNoPolisi + "\n"
                        + "Tanggal Servis: " + vTanggalServis + "\n"
                        + "Status Oli: " + vStatusOli + "\n"
                        + "Catatan Servis: " + vCatatan_Servis);
                if (confirm == 0) {
                    servis.catatDataServisKendaraan();

                    JOptionPane.showMessageDialog(null, "Data Servis Berhasil di tambah");
                    frame_Servis.tf_KodeServis.setText(servis.cariMaxKodeServis());
                    frame_Servis.dateChooser_TanggalServis.setDate(null);
                    frame_Servis.comboBox_StatusOli.setSelectedIndex(0);
                    frame_Servis.ta_CatatanServis.setText("");

                    kendaraan.rubahStatServisChecker();
//                        kendaraan.changeStatusKendaraan();
                    // frame_Servis.tf_BiayaServis.setText("");
//                    List<Kendaraan> kendaraans = Kendaraan_List.getKendaraanList().getAllKendaraanServis();
//                    Kendaraan_List_TableModel kendaraan_TableModel = new Kendaraan_List_TableModel(kendaraans);
//                    frame_Servis.table_DataServis.setModel(kendaraan_TableModel);
                    List<Servis> kendaraans = Servis_List.getServis_List().getServisbyKendaraan(vNoPolisi);
                    Servis_TableModel servis_TableModel  = new Servis_TableModel(kendaraans);
                    frame_Servis.table_DataServis.setModel(servis_TableModel);


                } else {
                    JOptionPane.showMessageDialog(null, "Penambahan data servis digagalkan");
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Servis_Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(null, "Data Harus Terisi Semua");
        }

    }

    public static void main(String[] args) {
    }
}
